require("../../utils/util.js");

var a = require("../../requests/request.js"), t = require("../../utils/common.js");

getApp();

Page({
    data: {
        carpics: {},
        serialId: 0,
        loading: !1,
        pageindex: 1,
        pagesize: 30,
        positionid: 6,
        currentTab: 0,
        swiperTab: 0,
        allcount: 0,
        nodata: !1,
        hasRefesh: !1,
        imgurls: [],
        winHeight: 0
    },
    onLoad: function(a) {
        var t = this;
        wx.getSystemInfo({
            success: function(e) {
                t.setData({
                    winHeight: e.windowHeight,
                    serialId: a.serialid
                });
            }
        });
    },
    onReady: function() {
        this.refesh();
    },
    errImg: function(a) {
        var e = this;
        t.errImgFun(a, e, "http://res.app.yiche.com/wechatapp/async100x100.png");
    },
    swiperchange: function(a) {
        console.log(a);
        var t = this;
        if (this.data.currentTab === a.detail.current.toString() || this.data.currentTab === a.detail.current) return !1;
        this.setData({
            currentTab: a.detail.current,
            positionid: t.convertpositionid(a.detail.current.toString()),
            pageindex: 1,
            imgurls: [],
            carpics: []
        }), t.refesh();
    },
    loadmore: function() {
        var t = this, e = this;
        this.data.nodata ? e.setData({
            loading: !1,
            hasRefesh: !1
        }) : a.getCarPic(this.data.positionid, this.data.serialId, ++this.data.pageindex, this.data.pagesize, function(a) {
            if (console.log(a), "2" == a.Status) if (e.setData({
                allcount: a.Data.imgcount
            }), a.Data.list.length > 0) {
                for (var s = [], i = 0; i < a.Data.list.length; i++) a.Data.list[i].ImageUrl && (a.Data.list[i].ImageUrl = a.Data.list[i].ImageUrl.replace("{0}", 1), 
                s.push(a.Data.list[i].ImageUrl.replace("_1.", "_3.")));
                e.setData({
                    carpics: e.data.carpics.concat(a.Data.list),
                    hasRefesh: e.data.allcount > a.Data.list.length,
                    hasMore: !1,
                    imgurls: e.data.imgurls.concat(s),
                    nodata: 0 == e.data.carpics.length && e.data.allcount < t.data.pageindex * t.data.pagesize
                });
            } else e.setData({
                carpics: e.data.carpics,
                hasRefesh: !1,
                nodata: 0 == e.data.carpics.length && e.data.allcount < t.data.pageindex * t.data.pagesize
            }); else e.setData({
                carpics: [],
                hasRefesh: !1,
                nodata: 0 == e.data.carpics.length
            });
        }, null, function() {
            e.setData({
                loading: !1
            });
        });
    },
    preview: function(a) {
        var t = a.currentTarget.dataset.imgurl;
        console.log(t);
        var e = this;
        wx.previewImage({
            current: t.replace("_1.", "_3."),
            urls: e.data.imgurls,
            success: function(a) {
                console.log(a);
            },
            fail: function() {
                console.log("fail");
            },
            complete: function() {}
        });
    },
    convertpositionid: function(a) {
        switch (a) {
          case "0":
            return 6;

          case "1":
            return 7;

          case "2":
            return 8;

          case "3":
            return 12;

          case "4":
            return 11;
        }
        return 6;
    },
    swichNav: function(a) {
        var t = a.currentTarget.dataset.current, e = this;
        if (e.setData({
            positionid: e.convertpositionid(t)
        }), e.data.currentTab === t) return !1;
        e.setData({
            hasRefesh: !0
        }), e.setData({
            currentTab: t,
            pageindex: 1,
            imgurls: [],
            carpics: [],
            swiperTab: t
        }), e.refesh();
    },
    onPullDownRefresh: function() {
        this.refesh(), wx.stopPullDownRefresh();
    },
    refesh: function(t) {
        var e = this, s = this;
        s.setData({
            hasRefesh: !0
        }), a.getCarPic(this.data.positionid, this.data.serialId, this.data.pageindex, this.data.pagesize, function(a) {
            if (console.log(a), "2" == a.Status) {
                s.setData({
                    allcount: a.Data.imgcount
                });
                for (var t = 0; t < a.Data.list.length; t++) a.Data.list[t].ImageUrl && (a.Data.list[t].ImageUrl = a.Data.list[t].ImageUrl.replace("{0}", 1), 
                s.data.imgurls.push(a.Data.list[t].ImageUrl.replace("_1.", "_3.")));
                s.setData({
                    carpics: a.Data.list,
                    hasRefesh: s.data.allcount > a.Data.list.length,
                    nodata: 0 == a.Data.list && s.data.allcount < e.data.pageindex * e.data.pagesize
                });
            } else e.setData({
                carpics: [],
                hasRefesh: !1,
                nodata: !1
            });
        }, null, function() {
            s.setData({
                loading: !1
            });
        });
    }
});